package bx.job.jobrouter.impl;

import bx.job.entity.BxJobWorkerInfo;
import bx.job.jobrouter.IJobWorkerRouter;

import java.util.Collections;
import java.util.List;
import java.util.Random;

public class JobWorkerRandomRouter implements IJobWorkerRouter {
    private static Random random = new Random();

    @Override
    public List<WorkerJobBindInfo> jobRoute(WorkerRouterContext workerRouterContext) {
        List<BxJobWorkerInfo> workerInfoList = workerRouterContext.getWorkerInfoList();
        BxJobWorkerInfo bxJobWorkerInfo = workerInfoList.get(random.nextInt(workerInfoList.size()));
        WorkerJobBindInfo jobBindInfo = WorkerJobBindInfo.builder()
                .bxJobInfo(workerRouterContext.getBxJobInfo())
                .bxJobWorkerInfo(bxJobWorkerInfo).build();
        return Collections.singletonList(jobBindInfo);
    }
}
